package com.atguigu.crowd.mvc.controller;

import com.atguigu.crowd.entity.Role;
import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.service.api.RoleService;
import org.aspectj.lang.annotation.AfterReturning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author zzqstart
 * @Description
 * @date 2022-09-06 2:11
 */
@Controller
public class AssignController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("assign/do/role/assgin.html")
    public String saveAdminRoleRelationship(
            @RequestParam("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword,
            @RequestParam(value = "roleIdList",required = false) List<Integer> roleIdList
    ) {
        roleService.saveAdminRoleRelationship(adminId,roleIdList);
        return "redirect:/admin/page.html?keyword="+keyword+"&pageNum="+pageNum;
    }

    @RequestMapping("assign/to/assign/role/page.html")
    public String toAssignRolePage(
            @RequestParam("adminId") Integer adminId,
            Model model
    ) {

        // 查询未分配的role
        List<Role> assignedRole = roleService.getAssignedRole(adminId);

        // 查询已分配的role
        List<Role> unAssignedRole = roleService.getUnAssignedRole(adminId);

        model.addAttribute("assignedRole", assignedRole);
        model.addAttribute("unAssignedRole", unAssignedRole);

        return "assgin-role";
    }
}
